package be.pluyckx.batteryprogress.utils.calculator;

/**
 * Created by Philip Luyckx on 8/08/13.
 */
public class AverageTimeProgressCalculator extends ProgressCalculator {

  public AverageTimeProgressCalculator() {
  }

  @Override
  public Result calculate(DataCollection data, Parameters params) {
    return calculate(data, params, 0);
  }

  /**
   * Calculate the avarage over the whole data collection
   *
   * @param data   The data collection
   * @param params The parameters
   * @param index  Ignored
   * @return
   */
  @Override
  public Result calculate(DataCollection data, Parameters params, int index) {
    if (data.size() < 2) {
      return null;
    }

    DataItem oldest;
    DataItem newest;

    if (params.mOrdered == Parameters.ORDERED_OLD_LOWEST_INDEX) {
      newest = data.getLastInCurrentState();
      oldest = data.getFirstInCurrentState();
    } else {
      newest = data.getFirstInCurrentState();
      oldest = data.getLastInCurrentState();
    }

    return average(oldest, newest);
  }
}
